import Common from "../common/Common";
import ViewBase from "./ViewBase";
import UserInfo from "../UserInfo";
import LevelInfo from "../Levellnfo";
import UIManager from "../manager/UIManager";
import Define from "../common/Define";
import ParticleEffectManager from "../manager/ParticleEffectManager";
import WXHelper from "../common/WXHelper";
import GameManager from "../manager/GameManager";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class UpgradeViewCtr extends ViewBase {


    @property(cc.Label)
    textDesc1: cc.Label = null;

    @property(cc.Label)
    textDesc2: cc.Label = null;

    @property(cc.Label)
    textUpgradeDesc: cc.Label = null;
    
    @property(cc.Label)
    textUpgradeSpend: cc.Label = null;

    //btns -------- 开始
    @property(cc.Node)
    btn_speed: cc.Node = null;
 
    @property(cc.Node)
    btn_gold: cc.Node = null;
 
    @property(cc.Node)
    btn_fire: cc.Node = null;
 
    @property(cc.Node)
    btn_offline: cc.Node = null;
 
    @property(cc.Node)
    btn_upgrade: cc.Node = null;

    @property(cc.Node)
    btn_vedio: cc.Node = null;

    @property(cc.Node)
    btn_share: cc.Node = null;

    @property(cc.Node)
    nodeSpeedDark: cc.Node = null;
 
    @property(cc.Node)
    nodeGoldDark: cc.Node = null;
 
    @property(cc.Node)
    nodeFireDark: cc.Node = null;
 
    @property(cc.Node)
    nodeOfflineDark: cc.Node = null;

    @property(cc.Node)
    nodeBgs: cc.Node = null;

    @property(cc.Node)
    nodebtnColors: cc.Node = null;

    @property(cc.Node)
    nodeGuideArrow: cc.Node = null;

    @property(cc.Label)
    textGuideDesc: cc.Label = null;

    @property(cc.Node)
    node_addHome: cc.Node = null;
 
    private curTag:string = "btn_speed";

    isClickShare:boolean = false;

    private upgradeSpeedSpendGold:number = 0;
    private upgradeFireSpendGold:number = 0;
    private upgradeGoldSpendGold:number = 0;
    private upgradeOfflineSpendGold:number = 0;
    //刷新UI 
    refreshView(isFristRefresh:boolean = false){
        console.log("刷新ui。。。。。。。。。。。。");
        this.node.children[0].stopAllActions();
        this.node.children[0].position = new cc.Vec2(320,-180)
        this.node.children[0].runAction(cc.moveTo(0.3,new cc.Vec2(320,158)))

        this.isClickShare = false;
    
        this.upgradeFireSpendGold = LevelInfo.getUpgradeNeedGold(UserInfo.firePower + 1);
        this.upgradeSpeedSpendGold = LevelInfo.getUpgradeNeedGold(UserInfo.bulletShootMaxCount - 2)
        this.upgradeGoldSpendGold = LevelInfo.getUpgradeNeedGold(UserInfo.goldDropLevel + 1);
        this.upgradeOfflineSpendGold = LevelInfo.getUpgradeNeedGold(UserInfo.offLineLevel + 1);

        this.onClick(this.curTag)
        this.handlerGuideArrow();

        UIManager.getInstance().executeEvent(Define.viewBattle,"hidePrompt")
        if(Common.curLevel == 4 && UserInfo.showAddHomeTag == 0){
            UserInfo.saveShowAddHome();
            UserInfo.showAddHomeTag = 1
            this.node_addHome.active = true
            this.node_addHome.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.2,new cc.Vec2(0,5)),cc.moveBy(0.2,new cc.Vec2(0,-5)))))
        }else{
            this.node_addHome.active = false;
        }
    }

    hideView(){
        WXHelper.showBannder(true)
        super.hideView()
    }

    //点击点击事件
    addEvent(){
        Common.addClickEvent(this.btn_speed,this.onClick.bind(this));
        Common.addClickEvent(this.btn_fire,this.onClick.bind(this));
        Common.addClickEvent(this.btn_gold,this.onClick.bind(this));
        Common.addClickEvent(this.btn_offline,this.onClick.bind(this));
        Common.addClickEvent(this.btn_upgrade,this.onClick.bind(this));
        Common.addClickEvent(this.btn_vedio,this.onClickVideo.bind(this));
        Common.addClickEvent(this.btn_share,this.onClickShare.bind(this));
        
    }

    onClickVideo(tag:string){
        console.log("onClickVideo")
        WXHelper.showVideo(function(state){
            if(state == 1){
                if(state == 1 || state == "1"){
                    this.upgradeSkills()
                }else{
                    console.log("state = " + state)
                }
            }
        }.bind(this))
    }

    onClickShare(tage:string){
        console.log("onClickShare")
        this.isClickShare = true
        GameManager.instance.wxHelper.shareAppMessage("");
    }

    onClick(tag:string){
    
        if(tag == "btn_upgrade"){

        }else{
            this.curTag  = tag;
        }
        this.refreshButtonStates(tag == "btn_upgrade");
    }

    upgradeSkills(){
        if(this.curTag == "btn_speed"){
            UserInfo.saveBulletShootMaxCount(1);
        }else if(this.curTag == "btn_fire"){
            UserInfo.saveFirePower();
        }else if(this.curTag == "btn_gold"){
            UserInfo.saveDropGoldLevel();
        }else if(this.curTag == "btn_offline"){
            UserInfo.saveOffLineLevel();
        }
        this.refreshButtonStates(false);
    }
    //刷新按键的状态
    refreshButtonStates(isUpgrade:boolean){

        for (let index = 0; index < this.nodeBgs.children.length; index++) {
            this.nodeBgs.children[index].active = false;
        }
        let tempindex = 0;

        let curLevel:number = 0;
        let spendGold:number = 0;

        let isUpgradeSucceed:boolean = false
        let effectColor:cc.Color = null;

        if(this.curTag == "btn_speed"){
            curLevel = UserInfo.bulletShootMaxCount - 2;
            spendGold = LevelInfo.getUpgradeNeedGold(curLevel);
            this.upgradeSpeedSpendGold = spendGold
            this.textDesc1.string = "开火速度";
            console.log("spendGold = "+spendGold)
            console.log("UserInfo.curGold = "+UserInfo.curGold)
            if(isUpgrade && UserInfo.curGold >= spendGold){
                UserInfo.saveGoldData(spendGold *-1) //减少金币数量
                UserInfo.saveBulletShootMaxCount(1);
                isUpgradeSucceed = true
                effectColor = new cc.Color(255,90,0);
            }
            this.textDesc2.string = (UserInfo.bulletShootMaxCount + 1).toString() + "发/秒"
        }else if(this.curTag == "btn_fire"){
            curLevel = UserInfo.firePower + 1
            spendGold = LevelInfo.getUpgradeNeedGold(curLevel);
            this.upgradeFireSpendGold = spendGold
            this.textDesc1.string = "火力";
            if(isUpgrade && UserInfo.curGold >= spendGold){
                UserInfo.saveGoldData(spendGold *-1) //减少金币数量
                UserInfo.saveFirePower();
                isUpgradeSucceed = true
                effectColor = new cc.Color(202,9,232);
            }
            this.textDesc2.string = ((UserInfo.firePower)*10 + 100) +"%";
            tempindex = 1
        }else if(this.curTag == "btn_gold"){
            curLevel = UserInfo.goldDropLevel + 1
            spendGold = LevelInfo.getUpgradeNeedGold(curLevel);
            this.upgradeGoldSpendGold = spendGold
            this.textDesc1.string = "金币";
            if(isUpgrade && UserInfo.curGold >= spendGold){
                UserInfo.saveGoldData(spendGold *-1) //减少金币数量
                UserInfo.saveDropGoldLevel();
                isUpgradeSucceed = true
                effectColor = new cc.Color(9,122,255);
            }
            this.textDesc2.string = ((UserInfo.goldDropLevel)*10 + 100) +"%";
            
            tempindex = 2
        }else if(this.curTag == "btn_offline"){

            curLevel = UserInfo.offLineLevel + 1
            spendGold = LevelInfo.getUpgradeNeedGold(curLevel);
            this.upgradeOfflineSpendGold = spendGold
            this.textDesc1.string = "离线";
            if(isUpgrade && UserInfo.curGold >= spendGold){
                UserInfo.saveGoldData(spendGold *-1) //减少金币数量
                UserInfo.saveOffLineLevel();
                isUpgradeSucceed = true
                effectColor = new cc.Color(255,220,3);
            }
            this.textDesc2.string = (UserInfo.offLineLevel*10 + 100) +"%";
            tempindex = 3
        }
        console.log("curLevel =============" + curLevel);
        let isShowVideo:boolean = false
        if(curLevel-1 > 0 && (curLevel-1)%10 == 0 && WXHelper.isLoadVideoSuccessful){
            this.btn_vedio.stopAllActions()
            this.btn_vedio.active = true
            isShowVideo = true
            Common.actionLeftRightRotate(this.btn_vedio,0.15,18,3)
        }else{
            this.btn_vedio.active = false
        }

        if(curLevel-1 > 0 && (curLevel-1)%4 == 0  && !isShowVideo){
            this.btn_share.stopAllActions()
            this.btn_share.active = true
            Common.actionLeftRightRotate(this.btn_share,0.15,18,3)
        }else{
            this.btn_share.active = false
        }

        if(isUpgrade && UserInfo.curGold < spendGold){
            Common.showPrompt("金币不足");
            this.btn_upgrade.stopAllActions();
            this.btn_upgrade.rotation = 0;
        }
        if(UserInfo.curGold >= spendGold){
            this.btn_upgrade.stopAllActions()
            Common.actionLeftRightRotate(this.btn_upgrade)
        }else{
            this.btn_upgrade.stopAllActions();
            this.btn_upgrade.rotation = 0;
        }

        if(isUpgradeSucceed){
            ParticleEffectManager.getInstance().showParticleEffect(5,new cc.Vec2(40,0),1,effectColor,this.textDesc2.node)
        }


        if(UIManager.getInstance().isShow(Define.viewMain)){
            UIManager.getInstance().executeEvent(Define.viewMain);
        }
       
        this.textUpgradeSpend.string = Common.getShowNumber(spendGold);
        this.nodeBgs.children[tempindex].active = true;

        //修改按键的图片
        let buttonSpriteFrame = this.nodebtnColors.children[tempindex].getComponent<cc.Sprite>(cc.Sprite).spriteFrame;
        this.btn_upgrade.getComponent<cc.Sprite>(cc.Sprite).spriteFrame = buttonSpriteFrame;
    
        this.nodeSpeedDark.active = false;
        this.nodeFireDark.active = false;
        this.nodeGoldDark.active = false;
        this.nodeOfflineDark.active = false;

        this.btn_fire.active = false;
        this.btn_gold.active = false;
        this.btn_offline.active = false;

        /*
        功能开启规则			
        【开火速度】升级到10级的时候，开启【火力】功能			
        【火力】升级到2级的时候，开启【金币】功能			
        【金币】升级到2级的时候，开启【离线】	 
        */

        //升级到第5颗子弹才能开启下一个技能
        if(UserInfo.bulletShootMaxCount > 12){
            this.btn_fire.active = true;
        }else{
            this.nodeFireDark.active = true;
        }
        if(UserInfo.firePower > 1){
            this.btn_gold.active = true;
        }else{
            this.nodeGoldDark.active = true;
        }

        if(UserInfo.goldDropLevel > 1){
            this.btn_offline.active = true;
        }else{
            this.nodeOfflineDark.active = true;
        }
        Common.curLevelGold = UserInfo.curGold;
        this.handlerGuideArrow();
        UIManager.getInstance().refreshGoldInfo(Define.viewBattle);
        UIManager.getInstance().refreshGoldInfo(Define.viewMain);
    }

    //处理引导箭头
    handlerGuideArrow(){

        let tempNode = null;
        //前期引导
        //引导升级速度

        if(UserInfo.curLevel >= 2 && UserInfo.bulletShootMaxCount <= 3 && UserInfo.curGold >= this.upgradeSpeedSpendGold)
        {
            if(this.curTag != "btn_speed"){
                tempNode = this.btn_speed;
            }else{
                tempNode = this.btn_upgrade;
            }
        }else if(UserInfo.bulletShootMaxCount >= 12 && this.btn_fire.active && UserInfo.firePower == 0 && UserInfo.curGold >= this.upgradeFireSpendGold){
            if(this.curTag != "btn_fire"){
                tempNode = this.btn_fire;
            }else{
                tempNode = this.btn_upgrade;
            }
        }else if(UserInfo.firePower > 1 && UserInfo.goldDropLevel == 1 &&  UserInfo.curGold >= this.upgradeGoldSpendGold){
            if(this.curTag != "btn_gold"){
                tempNode = this.btn_gold;
            }else{
                tempNode = this.btn_upgrade;
            }
        }else if(UserInfo.goldDropLevel > 1 && UserInfo.offLineLevel == 0 &&  UserInfo.curGold >= this.upgradeOfflineSpendGold){
            if(this.curTag != "btn_offline"){
                tempNode = this.btn_offline;
            }else{
                tempNode = this.btn_upgrade;
            }
        }

        //后期引导
        if(tempNode == null){
            //提示下一个等级
            if(UserInfo.bulletShootMaxCount - (UserInfo.firePower+2) >= 5 && UserInfo.firePower <= 20 &&
             UserInfo.firePower > 1 && UserInfo.curGold >= this.upgradeFireSpendGold){
                if(this.curTag != "btn_fire"){
                    tempNode = this.btn_fire;
                }else{
                    tempNode = this.btn_upgrade;
                }
    
            }else if(UserInfo.firePower - UserInfo.goldDropLevel >= 10 &&  UserInfo.goldDropLevel <= 20 && UserInfo.curGold >= this.upgradeGoldSpendGold){
                if(this.curTag != "btn_gold"){
                    tempNode = this.btn_gold;
                }else{
                    tempNode = this.btn_upgrade;
                }
            }else if(UserInfo.goldDropLevel - UserInfo.offLineLevel >= 10 && UserInfo.offLineLevel <= 20 && UserInfo.curGold >= this.upgradeOfflineSpendGold){
                if(this.curTag != "btn_offline"){
                    tempNode = this.btn_offline;
                }else{
                    tempNode = this.btn_upgrade;
                }
            }
        }

        /*
        在全部功能开启的情况下													
        【开火速度】与【火力】等级相差5级的时候，提示提升等级较低的													
        【金币】【离线】与【火力】相差10级的时候，提示升级等级较低的一个功能													
            若【金币】【离线】等级相同，则优先提示【金币升级】												
        只有点击功能分页时，不显示文字，每次指引升级按钮时，都显示文字描述		
        */											

        /*
        增加子弹数量
        增加子弹威力
        获得更多金币
        提升离线奖励
        */

        console.log(tempNode)
        if(tempNode != null){

            if(tempNode == this.btn_upgrade){
                if(this.curTag == "btn_speed"){
                    this.textGuideDesc.string = "增加子弹数量"
                }else if(this.curTag == "btn_fire"){
                    this.textGuideDesc.string = "增加子弹威力"
                }else if(this.curTag == "btn_gold"){
                    this.textGuideDesc.string = "获得更多金币"
                }else if(this.curTag = "btn_offline"){
                    this.textGuideDesc.string = "提升离线奖励"
                }
            }
            else{
                this.textGuideDesc.string = ""
            }
            this.nodeGuideArrow.stopAllActions();
            this.nodeGuideArrow.active = true;
            this.nodeGuideArrow.position = new cc.Vec2(tempNode.position.x,tempNode.position.y + 150);
            let x = this.nodeGuideArrow.position.x;
            let y = this.nodeGuideArrow.position.y;
            let action = cc.sequence(cc.moveTo(0.5,new cc.Vec2(x,y - 20)),cc.moveTo(0.5,new cc.Vec2(x,y + 10)))
            this.nodeGuideArrow.runAction(cc.repeatForever(action));
        }else{
            this.nodeGuideArrow.stopAllActions();
            this.nodeGuideArrow.active = false;
        }
    }
    //额外的事件调用

    executeEvent(eventTag:string = "defualt"){
        //在升级界面点击分享
        if(this.isClickShare){
            this.isClickShare = false
            this.upgradeSkills();
        }
    }
}
